#include "switch.h"

QVector<int> visited(max_col * max_row);
QVector<Node*> hnode;
Switch::Switch(QObject *parent)
    : QObject{parent}
{}

Switch::~Switch()
{
    foreach (Node *node, hnode) {
        delete node;
    }
    hnode.clear();
}

void Switch::switchPhase(const MazeData &mazeData)
{
    hnode.clear();
    record.clear();
    // number = 0;

    // 使用传入的迷宫数据
    const QVector<QVector<int>> &maze = mazeData.maze;

    int rows = maze.size() - 2;
    int cols = maze[0].size() - 2;


    // 初始化 hnode 和 record
    for (int i = 0; i <= rows+1; i++) {
        for (int j = 0; j <= cols+1; j++) {
            if (maze[i][j] == 0) {
                QPoint xy(i, j);
                Node* newNode = new Node(xy, hnode.size());
                hnode.push_back(newNode); // 可通行节点
                record.append(0);
                // number++;
            } else {
                record.append(1);  // 障碍或边界
            }
        }
    }

    // qDebug()<<hnode.size();

    int n = 0;
    int gap = 1;
    Node* p = NULL;
    for (int c = 0; c < record.size(); c++)
    {
        if (record[c] == 0)
        {

            if (c >= 2 * (cols + 2))//上方
            {
                if (record[c - cols - 2] == 0)
                {
                    for (int d = c - cols - 1; d < c; d++)
                    {
                        if (record[d] == 0)
                        {
                            gap++;
                        }
                    }
                    Node* new_node = new Node(hnode[n - gap]->xy, hnode[n - gap]->val); // 创建一个新节点
                    new_node->next = NULL; // 将新节点的 next 指针指向 hnode[n - gap]
                    hnode[n]->next = new_node; // 将 hnode[n] 的 next 指针指向新创建的节点
                }
                gap = 1;
            }

            if (record[c - 1] == 0)//左
            {
                Node* new_node = new Node(hnode[n - 1]->xy, hnode[n - 1]->val);
                new_node->next = NULL;
                p = hnode[n];
                while (p->next != NULL)
                {
                    p = p->next;
                }
                p->next = new_node;
            }
            if (record[c + 1] == 0)//右
            {
                Node* new_node = new Node(hnode[n + 1]->xy, hnode[n + 1]->val);
                new_node->next = NULL;
                p = hnode[n];
                while (p->next != NULL)
                {
                    p = p->next;
                }
                p->next = new_node;
            }
            if (c <= (cols + 2) * rows - 2)//下方
            {
                if (record[c + cols + 2] == 0)
                {
                    for (int d = c + 1; d < c + cols + 2; d++)
                    {
                        if (record[d] == 0)
                        {
                            gap++;
                        }
                    }
                    Node* new_node = new Node(hnode[n + gap]->xy, hnode[n + gap]->val);
                    new_node->next = NULL;
                    p = hnode[n];
                    while (p->next != NULL)
                    {
                        p = p->next;
                    }
                    p->next = new_node;
                }
                gap = 1;
            }
            n++;
        }
    }
}

// MazeData mazeData;
void Switch::get_b()
{
    int beg = -1;
    qDebug()<<hnode.size();
    for (int a = 0; a < hnode.size(); a++)
    {
        //qDebug()<<hnode[a]->val;

        if (hnode[a]->xy == mazeData.start )
        {
            // qDebug()<<mazeData.start.x()<<mazeData.start.y();
            beg = hnode[a]->val;
            begin = beg;
            // qDebug()<<hnode.size();
            // qDebug()<<begin;
            // qDebug()<<hnode[a]->xy.x()<<
            // hnode[a]->xy.y();
            break;
        }
    }
}

void Switch::get_e()
{
    int endi = -1;
    for (int a = 0; a < hnode.size(); a++)
    {
        if (hnode[a]->xy == mazeData.end)
        {
            endi = hnode[a]->val;
            ending = endi;
            break;
        }
    }
}

void Switch::reset_visited()
{
    for (int h = 0; h < hnode.size(); h++)
    {
         visited[h] = 0;
    }
}
